<template>
	<view>
		<form>
			<view class="cu-form-group">
				<view class="title">编号</view>
				<input maxlength="50" type="text" v-model="livestock_tag_number" placeholder="请输入编号" name="input" style="text-align: right; margin-right: -10rpx;"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">性别</view>
				<picker @change="GenderPickerChange" :value="index" :range="genderPicker">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">
						{{changeIndex>-1?genderPicker[index]:'请选择性别'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">出生种类</view>
				<picker @change="TypeChange" :value="index2" :range="typePicker">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">
						{{changeIndex2>-1?typePicker[index2]:'请选择出生种类'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">出生日期</view>
				<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
				    <view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">{{date}}</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">出生地</view>
				<input maxlength="50" type="text" v-model="birthplace" placeholder="请输入出生地" name="input" style="text-align: right; margin-right: -10rpx;"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">所在猪栏</view>
				<picker @change="PigstyChange" :value="index3" :range="pistyList">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">
						{{changeIndex3>-1?pistyList[index3]:'请选择猪栏'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">体重</view>
				<input maxlength="4" v-model="weight" type="digit" placeholder="请输入体重" name="input" style="text-align: right; margin-right: 20rpx;"></input>
				<text style="font-size: 26rpx;">kg</text>
			</view>
			
			
			<view class="cu-form-group">
				<view class="title">批次号</view>
				<picker @change="BatchNoChange" :value="index4" :range="batchNoPicker">
					<view class="picker" style="text-align: right; color: #808080; font-size: 30rpx; margin-right: -30rpx;">
						{{changeIndex4>-1?batchNoPicker[index4]:'请选择批次号'}}
					</view>
				</picker>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			const currentDate = this.getDate({
			format: true
			})
			return {
				isShow: false,
				isShow2: false,
				index: 0,
				changeIndex: -1,
				index2: 0,
				changeIndex2: -1,
				index3: 0,
				changeIndex3: -1,
				index4: 0,
				changeIndex4: -1,
				index5: 0,
				changeIndex5: -1,
				index6: 0,
				changeIndex6: -1,
				date: currentDate,
				genderPicker: ['公','母'],//性别picker选择列表
				typePicker: [],//种类类型picker选择列表
				pigstyPicker: [],//猪栏picker选择列表
				batchNoPicker: [],//批次号picker选择列表
				birthplace: '',//出生地
				weight: '',//体重
				pistyList: [],//猪栏总列表
				typeList: [],//类型列表
				changedPistyId: '' ,//被选中的猪栏id
				changedTypeId: 0 ,//类型id
				livestock_tag_number:''//编号
			}
		},
		onNavigationBarButtonTap() {
			this.submit()
		},
		onReady() {
			this.init();
			console.log(this.User);
			//获取猪栏
			this.getPigsty()
		},
		methods: {
			//初始化数据
			init () {
				//查询畜禽种类
				uni.request({
					url:this.confige.webUrl+'/livestock/listLivestockType',
					data:{
						
					},
					header:{
						"X-Token":this.User.token
					},
					success: (res) => {
						//console.log(res.data.result[0].list)
						this.typeList = res.data.result[0].list
						res.data.result[0].list.forEach((item,index) => {
							this.typePicker.push(item.type_name)
						})
					}
				})
				
				//查询批次号
				setTimeout(() => {
					uni.request({
						url:this.confige.webUrl+'/livestock/listLivestockBatch',
						data:{
							farm_id: this.User.userInfo.farm_id
						},
						header:{
							"X-Token":this.User.token
						},
						success: (res) => {
							console.log(res)
							if(res.data.code==200){
								res.data.result[0].list.forEach((item,index) => {
								this.batchNoPicker.push(item)
							})
							}
							else if(res.data.code==400){
								this.batchNoPicker.push("暂无批次号")
							}
							
						}
					})
				},300)
				//console.log(this.batchNoPicker)
			},
			//获取猪栏
			getPigsty(){
				uni.request({
					url:this.confige.webUrl+'/livestock/findAllPigsty',
					data:{
						farmId: this.User.userInfo.farm_id
					},
					header:{
						"X-Token":this.User.token
					},
					success: (res) => {
						console.log(res)
						this.pigstyPicker = res.data.result;
						let list = res.data.result;
						var arr = [];
						for(let i=0;i<list.length;i++){
							arr.push(list[i].pigsty_number)
							
						}
						console.log(arr)
							this.pistyList = arr
					}
				})
			},
			//性别选择器
			GenderPickerChange: function(e) {
				this.index = e.target.value
				this.changeIndex = this.index
				//console.log(this.index)
			},
			//出生种类选择器
			TypeChange: function(e) {
				this.index2 = e.target.value
				this.changeIndex2 = this.index2
				this.changedTypeId = this.typeList[this.index2].type_id
				//console.log(this.typeList)
			},
			//猪栏选择
			PigstyChange: function(e) {
				this.index3 = e.target.value
				this.changeIndex3 = this.index3
			},
			
			//批次号选择
			BatchNoChange: function(e) {
				this.index4 = e.target.value
				this.changeIndex4 = this.index4
				this.numberPicker = []
				this.index6 = 0
				this.changeIndex6 = -1
			},
			
			//日期选择
			bindDateChange: function(e) {
			    this.date = e.target.value
			},
			
			getDate(type) {
			    const date = new Date();
			    let year = date.getFullYear();
			    let month = date.getMonth() + 1;
			    let day = date.getDate();
			
			    if (type === 'start') {
			        year = year - 60;
			    } else if (type === 'end') {
			        year = year + 2;
			    }
			    month = month > 9 ? month : '0' + month;;
			    day = day > 9 ? day : '0' + day;
			    return `${year}-${month}-${day}`;
			},
			
			//提交
			submit () {
				uni.showLoading({
					title:"提交中"
				})
				if(this.changeIndex <= -1){
					uni.showToast({
						title:"请选择性别",
						icon: "none"
					})
					return;
				}
				else if(!this.livestock_tag_number){
					uni.showToast({
						title:"请填写编号",
						icon: "none"
					})
					return;
				}
				else if(this.changeIndex2 <= -1){
					uni.showToast({
						title:"请选择种类",
						icon: "none"
					})
					return;
				}
				else if(this.birthplace.toString() === ''){
					uni.showToast({
						title:"请填写出生地",
						icon: "none"
					})
					return;
				}
				else if(this.changeIndex3 <= -1){
					uni.showToast({
						title:"请选择猪栏",
						icon: "none"
					})
					return;
				}
				else if(this.birthplace.toString() === ''){
					uni.showToast({
						title:"请填写体重",
						icon: "none"
					})
					return;
				}
				else if(this.changeIndex4 <= -1){
					uni.showToast({
						title:"请选择批次号",
						icon: "none"
					})
					return;
				}
				else if(this.batchNoPicker[this.index4] ==="暂无批次号"){
					uni.showToast({
						title:"你选择了无效的批次号",
						icon: "none"
					})
					return;
				}
				else{
					
					uni.request({
						url:this.confige.webUrl+'/livestock/listLivestockAdd',
						data:{
							livestock_tag_number:this.livestock_tag_number,
							livestock_gender: this.genderPicker[this.index],//性别
							livestock_type: this.changedTypeId,//类型
							livestock_birth_date: this.date,//时间
							livestock_place: this.birthplace,//出生地
							pigsty_id:this.pigstyPicker[this.index3].pigsty_id,//猪栏id
							livestock_weight: this.weight,//重量
							batch_number: this.batchNoPicker[this.index4],//批次号
							// livestock_number: this.numberPicker[this.index6]
						},
						method: "POST",
						header:{
							"X-Token":this.User.token
						},
						success: (res) => {
							uni.hideLoading()
							console.log(res)
							if(res.data.code==200){
								uni.showModal({
									title: "提示",
									showCancel: false,
									content: "添加成功",
									success: (res) => {
										if(res.confirm){
										uni.switchTab({
										    url: '/pages/rear/rear'
										});
										}
									}
								})	
							}
							else if(res.data.code == 400) {
								uni.showModal({
									title: "提示",
									showCancel: false,
									content: res.data.message
								})
							}
						},
						fail(err) {
						uni.hideLoading()	
						}
					})
				}
			}
		},
		computed: {
		    startDate() {
		        return this.getDate('start');
		    },
		    endDate() {
		        return this.getDate('end');
		    }
		},
		onLoad() {
			
		},
		
	}
</script>

<style lang="less" scoped>
	@import "../../../common/colorui/main.css";
	@import "../../../common/colorui/icon.css";
	@import "../../../common/global.less";
	.submit {
		width: 710rpx;
		height: 80rpx;
		margin-top: 100rpx;
		margin-bottom: 30rpx;
	}
	
	.submit button {
		background-color: #4AC997;
		color: white;
		font-weight: bold;
		border-radius: 60rpx;
		margin-left: 40rpx;
		height: 80rpx;
		line-height: 80rpx;
	}
</style>
